package cn.tedu.controller;

import cn.tedu.vo.User;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    //    http://localhost:8080/springmvc1_war/user/reg.do
    @GetMapping("/reg.do")
    public ModelAndView reg() {
        System.out.println("显示reg模板");
        return new ModelAndView("reg");
    }

    //    http://localhost:8080/springmvc1_war/user/get.do?id=88
//    http://localhost:8080/springmvc1_war/user/get.do?id=88&if=666
    @GetMapping("/get.do")
    public String get(String id, @RequestParam("if") String ifv) {
        System.out.println("id:" + id);
        System.out.println("if:" + ifv);
        return "OK";
    }

    //http://localhost:8080/springmvc1_war/user/reg.do
    //http://localhost:8080/springmvc1_war/user/reg.do action="handle_reg.do"
    @PostMapping("/handle_reg.do")
    public String handleReg(User user, HttpServletRequest request) {
        System.out.println(user);
        String ip = request.getRemoteAddr();
        System.out.println("用户的IP:" + ip);
        return "OK";
    }

    public String handleReg(String username, String password, int age, String phone, String email) {
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        System.out.println("age:" + age);
        System.out.println("phone:" + phone);
        System.out.println("email:" + email);
        return "OK";
    }

    //http://localhost:8080/springmvc1_war/user/login.do
    @GetMapping("/login.do")
    public ModelAndView login() {
        System.out.println("显示登录页面");
        return new ModelAndView("login");
    }

    //    http://localhost:8080/springmvc1_war/user/login.do
//    @PostMapping("/handle_login.do")
    public ModelAndView handleLogin(String username, String password, HttpServletRequest request) {
        if (username.equals("robin")) {
            if (password.equals("123")) {
                //登录成功
                //将发送到用户界面（View）到数据存储到request对象中
                request.setAttribute("message", "欢迎您回来");
                return new ModelAndView("message");
            } else {
                //密码错误
                request.setAttribute("message", "密码错误！");
                return new ModelAndView("message");
            }
        } else {
            //用户名不存在
            request.setAttribute("message", "用户名不存在!");
            return new ModelAndView("message");
        }
    }

    //    http://localhost:8080/springmvc1_war/user/login.do
//    @PostMapping("/handle_login")
    public ModelAndView handleLogin(String username, String password) {
        if ("robin".equals(username)) {
            if ("123".equals(password)) {
                ModelAndView mv = new ModelAndView("message");
                mv.addObject("message", "欢迎您回来!");
                return mv;
            } else {
                ModelAndView mv = new ModelAndView("message");
                mv.addObject("message", "密码错误");
                return mv;
            }
        } else {
            ModelAndView mv = new ModelAndView("message");
            mv.addObject("message", "用户名错误!");
            return mv;
        }
    }

    //    http://localhost:8080/springmvc1_war/user/login.do
    @PostMapping("/handle_login")
    public ModelAndView handleLogin(String username, String password, ModelMap model, HttpSession session) {
        if ("robin".equals(username)) {
            if ("123".equals(password)) {
//                model.put("message", "欢迎回来!");
//                return new ModelAndView("message");
                session.setAttribute("loginuser", username);
                System.out.println("开始重定向...");
                return new ModelAndView("redirect:../home/index.do");
            } else {
                model.put("message", "密码错误!");
                return new ModelAndView("message");
            }
        } else {
            model.put("message", "用户名不存在!");
            return new ModelAndView("message");
        }
    }
}
